package cn.chenfeifx.checker.util;

import cn.chenfeifx.checker.eneity.ExportDataModel;
import cn.chenfeifx.checker.eneity.MessageEntity;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ImageBlankChecker {

    public static boolean isImageBlank(BufferedImage image, int threshold) {
        int width = image.getWidth();
        int height = image.getHeight();
        int blankColorRGB = image.getRGB(0, 0); // 假设空白页的像素颜色与图片左上角相同

        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                int rgb = image.getRGB(x, y);
                // 使用颜色差异度量方法，例如欧几里得距离
                if (Math.abs(rgb - blankColorRGB) > threshold) {
                    return false; // 发现非空白像素
                }
            }
        }
        return true; // 所有像素都是空白颜色
    }

    public static boolean isImageBlank(String imagePath, int threshold) {
        try {
            BufferedImage image = ImageIO.read(new File(imagePath));
            return isImageBlank(image, threshold);
        } catch (IOException e) {
            System.out.println("读取图片时发生IO异常：" + e.getMessage());
            return false;
        }
    }

    public static void checkSequenceContinuity(List<String> fileNames, String reference, List<MessageEntity> messageList, ExportDataModel exportDataModel) {
        System.out.println("图片顺序号排序检测开始");
        // 使用TreeMap保存文件名和序号
        List<String> sequenceList = new ArrayList<>();

        for (String fileName : fileNames) {
            String[] str = fileName.split("-");
            String s = str[str.length - 1];
            String substring = s.substring(0, s.length() - 4);
            sequenceList.add(substring.substring(substring.length() - 3));
        }

        Collections.sort(sequenceList);

        for (int i = 1; i < sequenceList.size(); i++) {
            int prevSeq = Integer.parseInt(sequenceList.get(i - 1));
            int currentSeq = Integer.parseInt(sequenceList.get(i));
            int poor = currentSeq - prevSeq;
            if (currentSeq != prevSeq + 1) {
                if (poor > 1) {
                    for (int j = 1; j < poor; j++) {
                        messageList.add(new MessageEntity(reference, "图片编号顺序不连续,缺少" + String.format("000%d", prevSeq + j)));
                        exportDataModel.setFileIntegrity("✖");
                    }
                } else {
                    messageList.add(new MessageEntity(reference, "图片编号顺序不连续,缺少" + String.format("000%d", prevSeq + 1)));
                    exportDataModel.setFileIntegrity("✖");
                }
            }
        }
        System.out.println("图片顺序号排序检测结束");
    }

    public static void main(String[] args) {
        String imagePath = "path/to/your/image.jpg";
        int colorThreshold = 50; // 颜色差异阈值，根据需要调整

        boolean isBlank = isImageBlank(imagePath, colorThreshold);
        if (isBlank) {
            System.out.println("图片是空白页");
        } else {
            System.out.println("图片不是空白页");
        }
    }
}